﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;

namespace NetWorkSet
{
    public partial class IPControl : UserControl
    {
        public IPControl()
        {
            InitializeComponent();
        }
        public enum IPType : byte { A, B, C, D, E };

        /// <summary>
        /// string类型的IP地址
        /// </summary>
        override public string Text
        {
            get
            {
                return this.Value.ToString();
            }
            set
            {
                IPAddress address;
                if (IPAddress.TryParse(value, out address))
                {
                    byte[] bytes = address.GetAddressBytes();
                    for (int i = 1; i <= 4; i++)
                    {
                        this.Controls["textBox" + i.ToString()].Text = bytes[i - 1].ToString("D");
                    }
                }
            }
        }

        /// <summary>
        /// IP地址
        /// </summary>
        public IPAddress Value
        {
            get
            {
                IPAddress address;
                string ipString = this.txtIP1.Text + "." + this.txtIP2.Text + "." + this.txtIP3.Text + "." + this.txtIP4.Text;

                if (IPAddress.TryParse(ipString, out address))
                {
                    return address;
                }
                else
                {
                    return new IPAddress(0);
                }
            }
            set
            {
                byte[] bytes = value.GetAddressBytes();
                //for (int i = 1; i <= 4; i++)
                //{
                //    this.Controls["txtIP" + i.ToString()].Text = bytes[i - 1].ToString("D");
                //}
            }
        }

        /// <summary>
        /// IP地址段分类，用于自动生成子网掩码判断
        /// </summary>
        public IPType Type
        {
            get
            {
                byte[] bytes = this.Value.GetAddressBytes();
                int FirstByte = bytes[0];

                if (FirstByte < 128)
                {
                    return IPType.A;
                }
                else if (FirstByte < 192)
                {
                    return IPType.B;
                }
                else if (FirstByte < 224)
                {
                    return IPType.C;
                }
                else if (FirstByte < 240)
                {
                    return IPType.D;
                }
                else
                {
                    return IPType.E;    // 保留做研究用
                }
            }
        }

        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns>IP</returns>
        public string[] GetIP()
        {
            string[] getWay = new string[4];
            getWay[0] = this.txtIP1.Text;
            getWay[1] = this.txtIP2.Text;
            getWay[2] = this.txtIP3.Text;
            getWay[3] = this.txtIP4.Text;

            return getWay;
        }

        /// <summary>
        /// 设置网关
        /// </summary>
        public void SetWay(string[] getWay)
        {
            this.txtIP1.Text = getWay[0];
            this.txtIP2.Text = getWay[1];
            this.txtIP3.Text = getWay[2];
            this.txtIP4.Text = getWay[3];
        }

        /// <summary>
        /// 获取默认网关
        /// </summary>
        /// <returns>网关</returns>
        public string[] GeteWay()
        {
            string[] getWay = new string[4];
            getWay[0] = this.txtIP1.Text;
            getWay[1] = this.txtIP2.Text;
            getWay[2] = this.txtIP3.Text;
            getWay[3] = "1";

            return getWay;
        }

        /// <summary>
        /// 控件的边框样式
        /// </summary>
        new public BorderStyle BorderStyle
        {
            get
            {
                return this.txtIP1.BorderStyle;
            }
            set
            {
                for (int i = 1; i <= 4; i++)
                {
                    ((TextBox)this.Controls["txtIP" + i.ToString()]).BorderStyle = value;
                }
            }
        }

        #region 文本框KeyPress事件

        int txtLen = 0;//用来文本框内文本长度
        private void txtIP1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //获取文本框内文本长度
            txtLen = ((TextBox)sender).TextLength;
            //判断只能输入数字
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;
            //如果输入“.”则跳到下个文本框
            if (e.KeyChar == 46)
            {
                this.txtIP2.Focus();
                this.txtIP2.SelectAll();
            }
        }

        private void txtIP2_KeyPress(object sender, KeyPressEventArgs e)
        {
            //获取文本框内文本长度
            txtLen = ((TextBox)sender).TextLength;
            //判断只能输入数字
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;
            //如果输入“.”则跳到下个文本框
            if (e.KeyChar == 46)
            {
                this.txtIP3.Focus();
                this.txtIP3.SelectAll();
            }
            //如果文本框内文本长度为0，按Backspace键则跳回上个文本框末尾
            if (e.KeyChar == 8 && txtLen == 0)
            {
                this.txtIP1.Focus();
                this.txtIP1.SelectionStart = 3;
            }
        }

        private void txtIP3_KeyPress(object sender, KeyPressEventArgs e)
        {
            //获取文本框内文本长度
            txtLen = ((TextBox)sender).TextLength;
            //判断只能输入数字
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;
            //如果输入“.”则跳到下个文本框
            if (e.KeyChar == 46)
            {
                this.txtIP4.Focus();
                this.txtIP4.SelectAll();
            }
            //如果文本框内文本长度为0，按Backspace键则跳回上个文本框末尾
            if (e.KeyChar == 8 && txtLen == 0)
            {
                this.txtIP2.Focus();
                this.txtIP2.SelectionStart = 3;
            }
        }

        private void txtIP4_KeyPress(object sender, KeyPressEventArgs e)
        {
            //获取文本框内文本长度
            txtLen = ((TextBox)sender).TextLength;
            //判断只能输入数字
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;
            //如果文本框内文本长度为0，按BackSpace键则跳回上个文本框末尾
            if (e.KeyChar == 8 && txtLen == 0)
            {
                this.txtIP3.Focus();
                this.txtIP3.SelectionStart = 3;
            }
        }

        #endregion

        #region 文本框TextChanged事件

        string txtStr = "";//文本框内的字符
        int strNum = 0;//文本框内的数的大小
        private void txtIP1_TextChanged(object sender, EventArgs e)
        {
            txtStr = ((TextBox)sender).Text;
            try
            {
                strNum = Convert.ToInt32(txtStr);
            }
            catch (Exception)
            {
                strNum = 0;
            }
            if ((txtStr.Length == 1 && strNum < 1) || (txtStr.Length == 3 && strNum > 223))
            {
                ShowMessage("提示", "请指定一个介于1与223之间的数字");
                this.txtIP1.Text = "223";
                this.txtIP1.SelectAll();
                return;
            }
        }

        private void txtIP2_TextChanged(object sender, EventArgs e)
        {
            txtStr = ((TextBox)sender).Text;
            try
            {
                strNum = Convert.ToInt32(txtStr);
            }
            catch (Exception)
            {
                strNum = 0;
            }
            if ((txtStr.Length == 1 && strNum < 0) || (txtStr.Length == 3 && strNum > 255))
            {
                ShowMessage("提示", "请指定一个介于0与255之间的数字");
                this.txtIP2.Text = "255";
                this.txtIP2.SelectAll();
                return;
            }
        }

        private void txtIP3_TextChanged(object sender, EventArgs e)
        {
            txtStr = ((TextBox)sender).Text;
            try
            {
                strNum = Convert.ToInt32(txtStr);
            }
            catch (Exception)
            {
                strNum = 0;
            }
            if ((txtStr.Length == 1 && strNum < 0) || (txtStr.Length == 3 && strNum > 255))
            {
                ShowMessage("提示", "请指定一个介于0与255之间的数字");
                this.txtIP3.Text = "255";
                this.txtIP3.SelectAll();
                return;
            }
        }

        private void txtIP4_TextChanged(object sender, EventArgs e)
        {
            txtStr = ((TextBox)sender).Text;
            try
            {
                strNum = Convert.ToInt32(txtStr);
            }
            catch (Exception)
            {
                strNum = 0;
            }
            if ((txtStr.Length == 1 && strNum < 0) || (txtStr.Length == 3 && strNum > 255))
            {
                ShowMessage("提示", "请指定一个介于0与255之间的数字");
                this.txtIP4.Text = "255";
                this.txtIP4.SelectAll();
                return;
            }
        }

        #endregion

        /// <summary>
        /// 文本框赋值
        /// </summary>
        /// <param name="ip">地址</param>
        public void SetTextBox(string[] ip)
        {
            this.txtIP1.Text = ip[0];
            this.txtIP2.Text = ip[1];
            this.txtIP3.Text = ip[2];
            this.txtIP4.Text = ip[3];
        }

        /// <summary>
        /// 弹出框
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="txt">文本</param>
        private void ShowMessage(string title, string txt)
        {
            MessageBox.Show(this, txt, title);
        }

        /// <summary>
        /// 判断文本框是否为空
        /// </summary>
        /// <returns></returns>
        public bool txtisNull()
        {
            if (this.txtIP1.Text.Trim() == "")
                return false;
            else if (this.txtIP2.Text.Trim() == "")
                return false;
            else if (this.txtIP3.Text.Trim() == "")
                return false;
            else if (this.txtIP4.Text.Trim() == "")
                return false;
            else
                return true;
        }

        /// <summary>
        /// 清空文本框
        /// </summary>
        public void txtClear()
        {
            this.txtIP1.Clear();
            this.txtIP2.Clear();
            this.txtIP3.Clear();
            this.txtIP4.Clear();
        }

        /// <summary>
        /// 获取文本框中的值
        /// </summary>
        /// <returns></returns>
        public string[] GetTextValuePra()
        {
            string[] str = new string[4];
            str[0] = this.txtIP1.Text.Trim();
            str[1] = this.txtIP2.Text.Trim();
            str[2] = this.txtIP3.Text.Trim();
            str[3] = this.txtIP4.Text.Trim();
            return str;
        }

        /// <summary>
        /// 获取文本框中的值
        /// </summary>
        /// <returns></returns>
        public string GetTextValueStr()
        {
            return this.txtIP1.Text.Trim() + "." + this.txtIP2.Text.Trim() + "." + this.txtIP3.Text.Trim() + "." + this.txtIP4.Text.Trim();
        }
    }
}
